﻿Imports System.Data.OleDb

Public Class mymessages
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim ID As Integer
        ID = Convert.ToInt32(Context.Request.QueryString("ID"))
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim dateString As String
        dateString = Date.Now.ToString("dd/MM/yyyy")
        Dim repSql As String = "SELECT Member FROM UserMessages WHERE ID=@f1"
        Dim repcmd As OleDbCommand = New OleDbCommand(repSql, oleDbConn)
        repcmd.Parameters.AddWithValue("@f1", ID)
        oleDbConn.Open()
        Dim user = repcmd.ExecuteScalar()
        '**user.identity.name throwing missing member exception so this user name to be obtained by workaround**
        Dim nameSql As String = "SELECT Recipient FROM UserMessages WHERE ID=@f1"
        Dim nameCmd As OleDbCommand = New OleDbCommand(nameSql, oleDbConn)
        nameCmd.Parameters.AddWithValue("@f1", ID)

        Dim thisuser = nameCmd.ExecuteScalar()

        Dim replySql As String = "Insert into UserMessages(Member,Message,PostTime,Recipient) Values (@f1,@f2,@f3,@f4)"
        Dim replycmd As OleDbCommand = New OleDbCommand(replySql, oleDbConn)

        replycmd.CommandType = CommandType.Text
        replycmd.Parameters.AddWithValue("@f1", thisuser)
        replycmd.Parameters.AddWithValue("@f2", TextBox1.Text)
        replycmd.Parameters.AddWithValue("@f3", dateString)
        replycmd.Parameters.AddWithValue("@f4", user)

        replycmd.ExecuteNonQuery()
        oleDbConn.Close()
        Response.Redirect("msgConfirm.aspx")
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim ID As Integer
        oleDbConn.Open()
        ID = Convert.ToInt32(Context.Request.QueryString("ID"))
        Dim delSql As String = "DELETE * FROM UserMessages WHERE ID=@f1"
        Dim delcmd As OleDbCommand = New OleDbCommand(delSql, oleDbConn)
        delcmd.Parameters.AddWithValue("@f1", ID)
        delcmd.ExecuteNonQuery()
        oleDbConn.Close()
        Response.Redirect("deleted.aspx")

    End Sub
End Class